package com.easy.controller;

import com.easy.bean.UserAccount;
import com.easy.service.UserAccountService;
import com.easy.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/register")
public class RegisterController {

    @Autowired
    private UserAccountService userAccountService;

    @PostMapping
    public Result register(@RequestBody UserAccount req) {
        UserAccount user = new UserAccount();

        // ✅ 修复：用 setPassword，而不是 setPassword_hash
        // 这样 service 会自动调用 PasswordUtil.encryptPassword 进行加密
        user.setUsername(req.getUsername());
        user.setPassword(req.getPassword());

        // 走 service 保存到数据库
        UserAccount createdUser = userAccountService.register(user);

        if (createdUser != null) {
            return Result.success("注册成功");
        } else {
            return Result.fail(500,"注册失败");
        }
    }
}